package com.wd.health.view.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wd.health.R;
import com.wd.health.model.adapter.CyypAdapter01;
import com.wd.health.model.adapter.CyypAdapter02;
import com.wd.health.model.bean.CyypBean;
import com.wd.health.model.bean.CyypBean02;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.view.activity.YpxqActivity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class CyypFragment extends Fragment {


    private RecyclerView rv01;
    private RecyclerView rv02;
    private int mid = 1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_cyyp, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        rv01 = view.findViewById(R.id.rv_cyyp01);
        rv02 = view.findViewById(R.id.rv_cyyp02);
        rv01.setLayoutManager(new LinearLayoutManager(getContext()));
        rv02.setLayoutManager(new GridLayoutManager(getContext(),3));

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initCyyp();
    }

    private void initCyyp02() {
        RetrofitManager.getInstance().getCyyp02Data(mid, 1, 15, new Observer<CyypBean02>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CyypBean02 cyypBean02) {
                List<CyypBean02.ResultBean> result = cyypBean02.getResult();
                CyypAdapter02 cyypAdapter02 = new CyypAdapter02(result, getContext());
                rv02.setAdapter(cyypAdapter02);
                cyypAdapter02.notifyDataSetChanged();
                cyypAdapter02.setGetListener(new CyypAdapter02.GetListener() {
                    @Override
                    public void onClick(String name, int id) {
                        Intent intent = new Intent(getActivity(), YpxqActivity.class);
                        intent.putExtra("id",id);
                        intent.putExtra("name",name);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initCyyp() {
        RetrofitManager.getInstance().getCyypData(new Observer<CyypBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CyypBean cyypBean) {
                List<CyypBean.ResultBean> list = cyypBean.getResult();
                final CyypAdapter01 cyypAdapter01 = new CyypAdapter01(list, getContext());
                rv01.setAdapter(cyypAdapter01);
                initCyyp02();
                cyypAdapter01.setGetListener(new CyypAdapter01.GetListener() {
                    @Override
                    public void onClick(int position, int id) {
                        cyypAdapter01.setmPosition(position);
                        mid = id;
                        initCyyp02();
                        cyypAdapter01.notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}